const KoaRouter = require('koa-router')
const HttpClient = require('../util/HttpClient')
const Template = require('../util/Template')

const router = new KoaRouter();
const modules = require('../../modules');

// api
router.get('/customer/view/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

router.get('/customer/create/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

router.get('/customer/edit/init', (ctx) => {
  return HttpClient.proxy(ctx);
});

router.get('/customer', async ctx => {
  let script = ['/customer.list.js'];
  let modConfig = modules['customer.list'];
  let reqHeaders = ctx.request.headers;
  let result = await HttpClient.request('/customer', 'get', null, { headers: reqHeaders });
  let body = result.body;
  
    ctx.body = await Template.renderWithHtml('客户管理', body, script, modConfig.template, ctx)
});

router.get('/customer/create', async ctx => {
  let modConfig = modules['customer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.js'];
  let result = await HttpClient.request('/customer/create', 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('新建客户', body, script, modConfig.template, ctx)
});

router.get('/customer/supplementCustomerInfo', async ctx => {
  let modConfig = modules['customer.edit1'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit1.js'];
  let result = await HttpClient.request(`/customer/supplementCustomerInfo`, 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('完善客户信息', body, script, modConfig.template, ctx)
});

router.get('/customer/edit/:id', async ctx => {
  let modConfig = modules['customer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.js'];
  let result = await HttpClient.request(`/customer/edit/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('编辑客户', body, script, modConfig.template, ctx)
});

router.get('/customer/register/create/:registerId', async ctx => {
  let modConfig = modules['customer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.js'];
  let result = await HttpClient.request(`/customer/register/create/${ctx.params.registerId}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('编辑客户', body, script, modConfig.template, ctx)
});

router.get('/customer/register/edit/:id/:registerId', async ctx => {
  let modConfig = modules['customer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.js'];
  let result = await HttpClient.request(`/customer/register/edit/${ctx.params.id}/${ctx.params.registerId}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('编辑客户', body, script, modConfig.template, ctx)
});

router.get('/event/createCustomer_v2', async ctx => {
  let modConfig = modules['customer.edit'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.js'];
  let result = await HttpClient.request(`/event/createCustomer_v2?eventId=${ctx.query.eventId}&goTo=${ctx.query.goTo}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('新建客户', body, script, modConfig.template, ctx)
});

router.get('/customer/view/:id', async ctx => {
  let modConfig = modules['customerV2.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customerV2.view.js'];
  let result = await HttpClient.request(`/customer/view/${ctx.params.id}`, 'get', null, { headers: reqHeaders });

    let body = result.body;
    ctx.body = await Template.renderWithHtml('客户信息', body, script, modConfig.template, ctx)
});

router.get('/customer/view/v2/:id', async ctx => {
  let modConfig = modules['customerV2.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customerV2.view.js'];
  let result = await HttpClient.request(`/customer/view/${ctx.params.id}`, 'get', null, { headers: reqHeaders });

    let body = result.body;
    ctx.body = await Template.renderWithHtml('客户信息', body, script, modConfig.template, ctx)
});

router.get('/customer/createOnTask', async ctx => {
  let modConfig = modules['customer.edit.modal'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.modal.js'];
  let result = await HttpClient.request('/customer/createOnTask', 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('新建客户', body, script, modConfig.template, ctx)
});

router.get('/customer/createOnEvent', async ctx => {
  let modConfig = modules['customer.edit.modal'];
  let reqHeaders = ctx.request.headers;
  let script = ['/customer.edit.modal.js'];
  let result = await HttpClient.request('/customer/createOnTask', 'get', null, { headers: reqHeaders });
  let body = result.body;

    ctx.body = await Template.renderWithHtml('新建客户', body, script, modConfig.template, ctx)
});

module.exports = router;